package me.nibo.popwindow.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ActionBarActivity {

    private PopupWindow popupWindow;
    private ListView listView;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    public void clickMe(View view) {
        Log.d("", "点击了我");
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.layout_menu, null);
        listView = (ListView) view.findViewById(R.id.lv_item);
        textView = (TextView) findViewById(R.id.tv_show);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("", "显示信息！！");
                if (popupWindow.isShowing()) {
                    Log.d("", "已显示");
                } else {
                    Log.d("", "未显示");
                    //
                    popupWindow.setFocusable(true);
                    // 设置允许在外点击消失
                    popupWindow.setOutsideTouchable(true);
                    // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
                    popupWindow.setBackgroundDrawable(new ColorDrawable());

                    popupWindow.showAsDropDown(textView, 0, 0);
                }
            }
        });
        List<String> dataList = new ArrayList<>();
        dataList.add("网络通信安全管理员-初级");
        dataList.add("网络通信安全管理员-中级");
        dataList.add("网络通信安全管理员-高级");
        dataList.add("网络通信安全管理员-技师");
        listView.setAdapter(new ArrayAdapter<>(this, R.layout.item_menu, R.id.tv_name, dataList));
        popupWindow = new PopupWindow(view, 400, 300);

        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
//        popupWindow.setBackgroundDrawable(new BitmapDrawable());

    }

    public void clickShowPopWindow(View view) {

    }
}
